package com.huika.dealontop.support.http;

import com.hk.app.android.lib.http.AjaxParams;
import com.hk.app.android.lib.http.params.help.ParamEncryptor;
import com.huika.dealontop.support.http.rsa.RsaHelper;

import org.apache.http.HttpEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.security.PublicKey;
import java.util.List;

public class JSONWrapAjaxParams extends AjaxParams {
    // 原来的数据通过json标签返回
    // @Override
    // protected void fillParams(final MultipartEntity multipartEntity) {
    // JSONObject jsonObject = convertParamToJSON();
    // multipartEntity.addPart("json", jsonObject.toString());
    // }
    //
    // @Override
    // protected List<BasicNameValuePair> getParamsList() {
    // List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>();
    //
    // JSONObject jsonObject = convertParamToJSON();
    //
    // lparams.add(new BasicNameValuePair("json", jsonObject.toString()));
    //
    // return lparams;
    // }
    //
    // private JSONObject convertParamToJSON() {
    // JSONObject jsonObject = new JSONObject();
    // for (HashMap.Entry<String, String> entry : getUrlParams().entrySet()) {
    // try {
    // jsonObject.put(entry.getKey(), entry.getValue());
    // }
    // catch (JSONException e) {
    // e.printStackTrace();
    // }
    // }
    // return jsonObject;
    // }

    /**
     * 只针对拼宝项目
     */
    private static String PUBLIC_KEY_VALUE = "<RSAKeyValue><Modulus>wVwBKuePO3ZZbZ//gqaNuUNyaPHbS3e2v5iDHMFRfYHS/bFw+79GwNUiJ+wXgpA7SSBRhKdLhTuxMvCn1aZNlXaMXIOPG1AouUMMfr6kEpFf/V0wLv6NCHGvBUK0l7O+2fxn3bR1SkHM1jWvLPMzSMBZLCOBPRRZ5FjHAy8d378=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
    public static PublicKey publicKey = RsaHelper.decodePublicKeyFromXml(PUBLIC_KEY_VALUE);

    public JSONWrapAjaxParams() {
        super();
        // 添加rsa加密字段
        initEncrypt(new ParamEncryptor() {
            @Override
            public String encrypt(Object o) {
                return RsaHelper.encryptDataFromStr(o.toString(), publicKey);
            }
        }, "password", "oldPassword", "newPassword");
        addEncrypt("userName");
        addEncrypt("passWord");
        addEncrypt("userId");
        addEncrypt("userID");
        addEncrypt("phone");
        addEncrypt("email");
        addEncrypt("activityId");
        addEncrypt("productId");
        addEncrypt("ProductId");
        addEncrypt("productID");
        addEncrypt("productNo");
        addEncrypt("payPwd");
        addEncrypt("amountByBalance");
        addEncrypt("amountByByOnline");
        addEncrypt("amountByOnline");
        addEncrypt("amountByDou");
        addEncrypt("account");
        addEncrypt("merchantId");
        addEncrypt("MerchantID");
        addEncrypt("merchantID");
        addEncrypt("evaluateID");
        addEncrypt("skuID");
        addEncrypt("orderId");
        addEncrypt("orderID");
        addEncrypt("quantity");
        addEncrypt("paymentOrderId");
        addEncrypt("PaymentOrderId");
        //addEncrypt("validate");
    }

    @Override
    protected HttpEntity getCommonEntity(List<BasicNameValuePair> paramsList) throws UnsupportedEncodingException {
        return new JSONEntity(paramsList);
    }
}
